package com.university.evaluation.controller;

import com.university.evaluation.enums.UploadEnum;
import com.university.evaluation.common.ResponseResult;
import com.university.evaluation.utils.FileUploadUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Ynchen
 * 文件上传控制器
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class FileUploadController {

    @Resource
    private FileUploadUtils fileUploadUtils;

    /**
     * 上传用户头像
     *
     * @param file 头像文件
     * @return 头像URL
     */
    @PostMapping("/upload/avatar")
    public ResponseResult<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
        try {
            log.info("接收到头像上传请求，文件名: {}, 文件大小: {}", file.getOriginalFilename(), file.getSize());
            
            if (file.isEmpty()) {
                log.error("上传文件为空");
                return ResponseResult.failure("上传文件为空");
            }
            
            String url = fileUploadUtils.upload(UploadEnum.USER_AVATAR, file);
            log.info("用户头像上传成功: {}", url);
            // 修复：确保数据在data字段，消息在msg字段
            return ResponseResult.success("头像上传成功", url);
        } catch (Exception e) {
            log.error("用户头像上传失败: {}", e.getMessage(), e);
            return ResponseResult.failure("头像上传失败: " + e.getMessage());
        }
    }

    /**
     * 通用文件上传接口
     *
     * @param file       文件
     * @param uploadType 上传类型 (USER_AVATAR, ARTICLE_COVER, etc.)
     * @return 文件URL
     */
    @PostMapping("/upload")
    public ResponseResult<String> uploadFile(@RequestParam("file") MultipartFile file,
                                   @RequestParam("uploadType") String uploadType) {
        try {
            UploadEnum uploadEnum = UploadEnum.valueOf(uploadType);
            String url = fileUploadUtils.upload(uploadEnum, file);
            log.info("文件上传成功: {} -> {}", uploadType, url);
            return ResponseResult.success(url, "文件上传成功");
        } catch (IllegalArgumentException e) {
            log.error("不支持的上传类型: {}", uploadType);
            return ResponseResult.failure("不支持的上传类型: " + uploadType);
        } catch (Exception e) {
            log.error("文件上传失败: {}", e.getMessage(), e);
            return ResponseResult.failure("文件上传失败: " + e.getMessage());
        }
    }

    /**
     * 删除文件
     *
     * @param fileUrl 文件URL
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    public ResponseResult<Boolean> deleteFile(@RequestParam("fileUrl") String fileUrl) {
        try {
            // 从URL中提取文件名和目录
            String fileName = fileUploadUtils.getFileName(fileUrl);
            // 这里需要根据URL结构解析出目录，简化处理
            String objectName = fileUrl.substring(fileUrl.indexOf("/blog/") + 6);
            String dir = objectName.substring(0, objectName.lastIndexOf("/") + 1);
            
            boolean success = fileUploadUtils.deleteFile(dir, fileName);
            if (success) {
                log.info("文件删除成功: {}", fileUrl);
                return ResponseResult.success("文件删除成功", true);
            } else {
                log.error("文件删除失败: {}", fileUrl);
                return ResponseResult.failure("文件删除失败");
            }
        } catch (Exception e) {
            log.error("文件删除异常: {}", e.getMessage(), e);
            return ResponseResult.failure("文件删除异常: " + e.getMessage());
        }
    }
}